package com.software.myopiaparents.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.software.myopiaparents.R;
import com.software.myopiaparents.content.Constant;
import com.software.myopiaparents.utils.ToastUtil;
import com.software.myopiaparents.utils.eb.EB;
import com.software.myopiaparents.utils.eb.EbData;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 微信支付回调
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String TAG = "WXPayEntryActivity";
    @BindView(R.id.wx_result)
    TextView wxResult;
    private IWXAPI api;

    private int vipType;  // 会员套餐 vip类型 1：年度会员  2：月度会员

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        ButterKnife.bind(this);
        EB.register(this);
        api = WXAPIFactory.createWXAPI(this, Constant.WX_APP_ID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
//                    Log.e(TAG, "微信支付返回0  resp.errCode === " + resp.errCode);
//                    ToastUtil.showShort(getResources().getString(R.string.s_payment_success));
//                    EB.post(new EbData(301));
//                    EB.post(new EbData(302));
//
//                    // 微信支付事件ID统计
//                    if (vipType == 1) {
//                        MobclickAgent.onEvent(WXPayEntryActivity.this, "vipWechatYear");
//                    } else if (vipType == 2) {
//                        MobclickAgent.onEvent(WXPayEntryActivity.this, "vipWechatMonth");
//                    }

                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    Log.e(TAG, "微信支付返回-2  resp.errCode === " + resp.errCode);
//                    ToastUtil.showShort(getResources().getString(R.string.s_payment_cancel));
                    break;
                default:
//                    ToastUtil.showShort(getResources().getString(R.string.s_payment_error));
                    break;
            }
            finish();
        }
    }

    @Subscribe
    public void onEvent(EbData data) {
        if (data.getType() == 501) {
            vipType = Integer.parseInt(data.getObj().toString());
            Log.e(TAG, "====vipType:" + vipType);
        }
    }


    @Override
    public void onDestroy() {
        EB.unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}